package cn.zhqr.lib.bean.response;


/**
 * 用于封装api接口返回的json数据
 * Created by charles on 2016/9/12.
 */
public class OutResponse<T> {
    /**
     * 错误码，参见ErrorCode.java
     */
    private int code;
    /**
     * 错误描述
     */
    private String msg;
    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 业务数据
     */
    private T data;

    public OutResponse(int code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
        if(code==0){
            this.success = true;
        }
    }

    public static OutResponse buildError(int code,String msg){
        return new OutResponse(code,msg,null);
    }
    public static OutResponse buildError(int code,String msg,Object data){
        return new OutResponse(code,msg,data);
    }
    public static OutResponse buildError(ErrorCode errorCode){
        return new OutResponse(errorCode.getCode(),errorCode.getMsg(),null);
    }
    public static OutResponse buildError(ErrorCode errorCode,String msg){
        return new OutResponse(errorCode.getCode(),msg,null);
    }
    public static OutResponse buildError(ErrorCode errorCode,String msg,Object data){
        return new OutResponse(errorCode.getCode(),msg,data);
    }

    /**
     * 构造成功的json对象
     * @param data
     * @return
     */
    public static OutResponse buildSuccess(String msg,Object data){
        return new OutResponse(ErrorCode.SUCCESS.getCode(),msg,data);
    }
    public static OutResponse buildSuccess(){
        return new OutResponse(ErrorCode.SUCCESS.getCode(),"Success",null);
    }
    public static OutResponse buildSuccess(Object data){
        return new OutResponse(ErrorCode.SUCCESS.getCode(),"Success",data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
